package com.spider.iceCream.spiderComponent;

import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.*;


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * @author LJb
 */
public class GetBarrage {
//    public static void main(String[] args) {
//        List<Barrage> barrages = new ArrayList<Barrage>();
////        System.out.println(barrages);
//        Scanner input = new Scanner(System.in);
//        String cid = input.next();
//        String urlStr="http://comment.bilibili.com/"+cid+".xml";
//
//        Document doc= Jsoup.parse(getHtml(urlStr));
////
//        //获取所有的d标签   也就是存放弹幕的标签
//        Elements d = (Elements) doc.getElementsByTag("d");
//        for(Element element: d){
//            Barrage barrage = new Barrage();
////            System.out.println("pre:"+(element.text()));
//            barrage.setMessage(element.text());
//            barrages.add(barrage);
//        }
//        for (Barrage b:barrages){
//            System.out.println(b.getMessage());
//        }
//    }
    public List<Barrage> getBarrages(Integer cid){
        List<Barrage> barrages = new ArrayList<Barrage>();
        String urlStr="http://comment.bilibili.com/"+cid+".xml";

        Document doc= Jsoup.parse(getHtml(urlStr));
        //获取所有的d标签   也就是存放弹幕的标签
        Elements d = (Elements) doc.getElementsByTag("d");
        for(Element element: d){
            Barrage barrage = new Barrage();
            barrage.setCid(cid);
            barrage.setMessage(element.text());
            barrages.add(barrage);
        }
        return barrages;
    }
    public static String getHtml(String url) {
        String html="";
        try {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = client.execute(httpget);

            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            html=inputStream2String(is);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return html;
    }
    public static String  inputStream2String(InputStream is)
            throws UnsupportedEncodingException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,
                "utf-8"));
        StringBuffer sb = new StringBuffer();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
